<?php 

    /**
     * 检查是否是以手机浏览器进入(IN_MOBILE)
     */
    function isMobile() {
        $mobile = array();
        static $mobilebrowser_list ='Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
        //note 获取手机浏览器
        if(preg_match("/$mobilebrowser_list/i", $_SERVER['HTTP_USER_AGENT'], $mobile)) {
            return true;
        }else{
            if(preg_match('/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER['HTTP_USER_AGENT'])) {
                return false;
            }else{
                if($_GET['mobile'] === 'yes') {
                    return true;
                }else{
                    return false;
                }
            }
        }
    }
    
    function isiPhone()
    {
        return strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false;
    }
    
    function isiPad()
    {
        return strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false;
    }
    
    function isiOS()
    {
        return isiPhone() || isiPad();
    }
    
    function isAndroid()
    {
        return strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false;
    }
    
    /**
     * 友好的时间显示
     *
     * @param int    $sTime 待显示的时间
     * @param string $type  类型. normal | mohu | full | ymd | other
     * @param string $alt   已失效
     * @return string
     */
    function friendlyDate($sTime,$type = 'normal',$alt = 'false') {
        if (!$sTime)
            return '';
        //sTime=源时间，cTime=当前时间，dTime=时间差
        $cTime      =   time();
        $dTime      =   $cTime - $sTime;
        $dDay       =   intval(date("z",$cTime)) - intval(date("z",$sTime));
        //$dDay     =   intval($dTime/3600/24);
        $dYear      =   intval(date("Y",$cTime)) - intval(date("Y",$sTime));
        //normal：n秒前，n分钟前，n小时前，日期
        if($type=='normal'){
            if( $dTime < 60 ){
                if($dTime < 10){
                    return '刚刚';    //by yangjs
                }else{
                    return intval(floor($dTime / 10) * 10)."秒前";
                }
            }elseif( $dTime < 3600 ){
                return intval($dTime/60)."分钟前";
                //今天的数据.年份相同.日期相同.
            }elseif( $dYear==0 && $dDay == 0  ){
                //return intval($dTime/3600)."小时前";
                return '今天'.date('H:i',$sTime);
            }elseif($dYear==0){
                return date("m月d日 H:i",$sTime);
            }else{
                return date("Y-m-d H:i",$sTime);
            }
        }elseif($type=='mohu'){
            if( $dTime < 60 ){
                return $dTime."秒前";
            }elseif( $dTime < 3600 ){
                return intval($dTime/60)."分钟前";
            }elseif( $dTime >= 3600 && $dDay == 0  ){
                return intval($dTime/3600)."小时前";
            }elseif( $dDay > 0 && $dDay<=7 ){
                return intval($dDay)."天前";
            }elseif( $dDay > 7 &&  $dDay <= 30 ){
                return intval($dDay/7) . '周前';
            }elseif( $dDay > 30 ){
                return intval($dDay/30) . '个月前';
            }
            //full: Y-m-d , H:i:s
        }elseif($type=='full'){
            return date("Y-m-d , H:i:s",$sTime);
        }elseif($type=='ymd'){
            return date("Y-m-d",$sTime);
        }else{
            if( $dTime < 60 ){
                return $dTime."秒前";
            }elseif( $dTime < 3600 ){
                return intval($dTime/60)."分钟前";
            }elseif( $dTime >= 3600 && $dDay == 0  ){
                return intval($dTime/3600)."小时前";
            }elseif($dYear==0){
                return date("Y-m-d H:i:s",$sTime);
            }else{
                return date("Y-m-d H:i:s",$sTime);
            }
        }
    }
    
    /**
     * 获取用户浏览器型号。新加浏览器，修改代码，增加特征字符串.把IE加到12.0 可以使用5-10年了.
     */
    function getBrowser(){
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'Maxthon')) {
            $browser = 'Maxthon';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 12.0')) {
            $browser = 'IE12.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 11.0')) {
            $browser = 'IE11.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 10.0')) {
            $browser = 'IE10.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 9.0')) {
            $browser = 'IE9.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0')) {
            $browser = 'IE8.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) {
            $browser = 'IE7.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.0')) {
            $browser = 'IE6.0';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'NetCaptor')) {
            $browser = 'NetCaptor';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape')) {
            $browser = 'Netscape';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx')) {
            $browser = 'Lynx';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera')) {
            $browser = 'Opera';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')) {
            $browser = 'Google';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
            $browser = 'Firefox';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')) {
            $browser = 'Safari';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'iphone') || strpos($_SERVER['HTTP_USER_AGENT'], 'ipod')) {
            $browser = 'iphone';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'ipad')) {
            $browser = 'iphone';
        } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'android')) {
            $browser = 'android';
        } else {
            $browser = 'other';
        }
        return $browser;
    }
    
    /**
     * 获取字串首字母
     * @param string $s0
     * @return string
     */
    function getFirstLetter($s0) {
        $firstchar_ord = ord(strtoupper($s0{0}));
        if($firstchar_ord >= 65 and $firstchar_ord <= 91) return strtoupper($s0{0});
        if($firstchar_ord >= 48 and $firstchar_ord <= 57) return '#';
        $s = iconv("UTF-8", "gb2312", $s0);
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if($asc>=-20319 and $asc<=-20284) return "A";
        if($asc>=-20283 and $asc<=-19776) return "B";
        if($asc>=-19775 and $asc<=-19219) return "C";
        if($asc>=-19218 and $asc<=-18711) return "D";
        if($asc>=-18710 and $asc<=-18527) return "E";
        if($asc>=-18526 and $asc<=-18240) return "F";
        if($asc>=-18239 and $asc<=-17923) return "G";
        if($asc>=-17922 and $asc<=-17418) return "H";
        if($asc>=-17417 and $asc<=-16475) return "J";
        if($asc>=-16474 and $asc<=-16213) return "K";
        if($asc>=-16212 and $asc<=-15641) return "L";
        if($asc>=-15640 and $asc<=-15166) return "M";
        if($asc>=-15165 and $asc<=-14923) return "N";
        if($asc>=-14922 and $asc<=-14915) return "O";
        if($asc>=-14914 and $asc<=-14631) return "P";
        if($asc>=-14630 and $asc<=-14150) return "Q";
        if($asc>=-14149 and $asc<=-14091) return "R";
        if($asc>=-14090 and $asc<=-13319) return "S";
        if($asc>=-13318 and $asc<=-12839) return "T";
        if($asc>=-12838 and $asc<=-12557) return "W";
        if($asc>=-12556 and $asc<=-11848) return "X";
        if($asc>=-11847 and $asc<=-11056) return "Y";
        if($asc>=-11055 and $asc<=-10247) return "Z";
        return '#';
    }
    
    /**
     +----------------------------------------------------------
     * 字符串截取，支持中文和其它编码
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $str 需要转换的字符串
     * @param string $length 截取长度
     * @param string $charset 编码格式
     * @param string $suffix 截断显示字符
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    function mStr($str, $length, $charset="utf-8", $suffix=true){
        return msubstr($str, 0, $length, $charset, $suffix);
    }
    /**
     +----------------------------------------------------------
     * 字符串截取，支持中文和其它编码
     +----------------------------------------------------------
     * @static
     * @access public
     +----------------------------------------------------------
     * @param string $str 需要转换的字符串
     * @param string $start 开始位置
     * @param string $length 截取长度
     * @param string $charset 编码格式
     * @param string $suffix 截断显示字符
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
        if(function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length, $charset);
        elseif(function_exists('iconv_substr')) {
            $slice = iconv_substr($str,$start,$length,$charset);
        }else{
            $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("",array_slice($match[0], $start, $length));
        }
        if($suffix && $str != $slice) return $slice."...";
        return $slice;
    }
    
    /**
     * 是否AJAX请求
     * @return bool
     */
    function isAjax() {
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
            if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
                return true;
        }
        if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
            return true;
        return false;
    }
    
    /**
     * 检查Email地址是否合法
     * @return boolean
     */
    function isValidEmail($email) {
        return preg_match("/^[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/i", $email) !== 0;
    }
    

    /**
     * 字节格式化 把字节数格式为 B K M G T 描述的大小
     * @return string
     */
    function byte_format($size, $dec=2) {
        $a = array("B", "KB", "MB", "GB", "TB", "PB");
        $pos = 0;
        while ($size >= 1024) {
            $size /= 1024;
            $pos++;
        }
        return round($size,$dec)." ".$a[$pos];
    }
    
    /**
     * 截取字符串长度
     * @param string $str 输入的字符串
     * @param string $length 截取的长度
     * @param string $ext 截取后，可以拼接的后缀
     * @return string
     */
    function getShort($str, $length = 40, $ext = '') {
        $str    =   htmlspecialchars($str);
        $str    =   strip_tags($str);
        $str    =   htmlspecialchars_decode($str);
        $strlenth   =   0;
        $out        =   '';
        preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/", $str, $match);
        foreach($match[0] as $v){
            preg_match("/[\xe0-\xef][\x80-\xbf]{2}/",$v, $matchs);
            if(!empty($matchs[0])){
                $strlenth   +=  1;
            }elseif(is_numeric($v)){
                //$strlenth +=  0.545;  // 字符像素宽度比例 汉字为1
                $strlenth   +=  0.5;    // 字符字节长度比例 汉字为1
            }else{
                //$strlenth +=  0.475;  // 字符像素宽度比例 汉字为1
                $strlenth   +=  0.5;    // 字符字节长度比例 汉字为1
            }
    
            if ($strlenth > $length) {
                $output .= $ext;
                break;
            }
    
            $output .=  $v;
        }
        return $output;
    }
    